from typing import Optional

from ..core.graph import FlowGraph
from .base_runner import BaseRunner


class LangGraphRunner(BaseRunner):
    """
    LangGraph 引擎运行器：复用 BaseRunner 的标准生命周期，只覆盖差异化部分。
    """

    def __init__(
        self,
        graph: FlowGraph,
        max_steps: int = 1000,
        checkpoint_file: Optional[str] = None,
        resume: bool = False,
        node_timeout: Optional[float] = None,
        parallelism: int = 1,
        flow_id: Optional[int] = None,
        run_id: Optional[int] = None,
    ):
        # 透传参数，保留并行度（若内部需要可使用）
        super().__init__(
            graph=graph,
            max_steps=max_steps,
            checkpoint_file=checkpoint_file,
            resume=resume,
            node_timeout=node_timeout,
            parallelism=parallelism,
            flow_id=flow_id,
            run_id=run_id,
        )

    def engine_name(self) -> str:
        return 'LangGraphRunner'

    def log(self, message: str) -> None:
        prefixed = f"[LangGraphRunner] {message}"
        print(prefixed)
        self.trace.append(prefixed)

    def request_stop(self) -> None:
        self.stop_requested = True